<?php 
namespace app\index\model;
use think\Model;
use think\File;      // 使用文件上传类
use think\Validate;  // 使用文件上传验证
use think\Request;   // 接值时使用
use think\Session;
use think\Db;
class Projectnotification_model extends Model
{
	//问题:分配人员
	public function assignee()
	{
		$model = Db::table('user')->select();
		return $model;
	}

	//问题:项目选择
	public function project_username()
	{
		$model = Db::table('project')->field('project_id,project_username')->select();
		return $model;
	}


	//发布通知:添加数据
	public function notice_add($params)
	{
		$data['notice_username']  = session('username');
		$data['notice_title']  	  = $params['notice_title'];
		$data['notice_text']   	  = $params['notice_text'];
		$data['notice_time']   	  = date('Y-m-d H:i:s',time());
		$arr = Db::table('notice')->insert($data);
		return $arr;
	}

	//新建项目
	public function version_add($params)
	{
		$where = array (															
            'project_username'=>$params['project_username'],
        );
		$sql = Db::table('project')->where($where)->find();
        if($sql) {	
        	echo "<script> alert('项目已经存在');location.href='version.html';</script>";die;
		}
		$data['project_username'] = $params['project_username'];
		$data['project_title']    = $params['project_title'];
		$data['project_type']     = $params['project_type'];
		$data['project_time']     = date('Y-m-d H:i:s',time());
		$data['problem_assignee'] = session('username');
		$data['project_planned_start_date']  = $params['project_planned_start_date'];
		$data['project_planned_end_date']    = $params['project_planned_end_date'];
		
		$arr = Db::table('project')->insert($data);
		return $arr;
	}

	//项目选择
	public function question()
	{
		$model = Db::table('project')->select();
		return $model;
	}

	/**
     * 单文件上传,项目添加
     */
    public function uploadOne($file,$params)
    {
        $filePath = ROOT_PATH . 'public' . DS . 'uploads'; // 项目物理路径
        $rootPath = Request::instance()->root(true);         // 项目根路径
        if (!file_exists($filePath)) {
            mkdir($filePath);
        }else{          
            $info = $file->Validate(['size'=>156780,'ext'=>'jpg,png,gif'])->move($filePath);
            if($info){
            	$info = "/uploads/".$info->getSaveName(); 
                // $info = $info->getFilename();
				$data['project_username'] = $params['project_username'];
				$data['problem_title'] 	  = $params['problem_title'];
				$data['problem_modules']  = $params['problem_modules'];
				$data['problem_note']     = $params['problem_note'];
				$data['problem_type']     = $params['problem_type'];
				$data['problem_priority'] = $params['problem_priority'];
				$data['problem_dealer']   = $params['problem_dealer'];
				$data['problem_assignee'] = $params['problem_assignee'];
				$data['problem_img'] 	  = $info;  //图片
				$data['problem_recurrence_method']  = $params['problem_recurrence_method']; //复现方式
				$data['problem_modified_person']    = $params['problem_modified_person'];
				$data['problem_change_time']    	= date('Y-m-d H:i:s',time());
				$data['problem_surroundings']       = $params['problem_surroundings'];
				$data['problem_change_the_time']    = $params['problem_change_the_time'];
				$data['problem_planned_start_date'] = $params['problem_planned_start_date'];
				$data['problem_planned_end_date']   = $params['problem_planned_start_date'];
				$data['problem_Discovery_Version']  = $params['problem_Discovery_Version'];
				$arr = Db::table('problem')->insert($data);

				$where = array (															
		            'problem_img'=>$info,
		        );
        		$sql = Db::table('problem')->where($where)->find();
				$datas['logs_operator'] 		  = session('username');
				$datas['problem_id'] 			  = $sql['problem_id'];
		        $datas['logs_operation_behavior'] = '创建任务';
		        $datas['logs_note'] 			  = '创建项目中出现的问题';
		        $datas['logs_operation_time']     = date('Y-m-d H:i:s',time());
				$arrr = Db::table('logs')->insert($datas);
				return $arr;
            }else{
                return $file->getError();
            }
        }
    }


    /**
     * 多文件上传
     */
    public function uploadAll($files)
    {
        $filePath = ROOT_PATH . 'public' . DS . 'uploads'; // 项目物理路径
        $rootPath = Request::instance()->root(true);         // 项目根路径
        $array = array();
        foreach ($files as $key => $file) {
            if (!file_exists($filePath)) {
                mkdir($filePath);
            }else{          
                $info = $file->Validate(['size'=>15678,'ext'=>'jpg,png,gif'])->move($filePath);
                if($info){
                    // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                    $imgPath = $rootPath."/uploads/".$info->getSaveName();   // 返回带域名的图片路径
                    array_push($array,$imgPath);

                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
                    $info = $info->getFilename();
                    $data['project_username'] = $params['project_username'];
					$data['problem_title'] 	  = $params['problem_title'];
					$data['problem_modules']  = $params['problem_modules'];
					$data['problem_note']     = $params['problem_note'];
					$data['problem_type']     = $params['problem_type'];
					$data['problem_priority'] = $params['problem_priority'];
					$data['problem_dealer']   = $params['problem_dealer'];
					$data['problem_assignee'] = $params['problem_assignee'];
					$data['problem_img'] 	  = $info;
					$data['problem_modified_person']    = $params['problem_modified_person'];
					$data['problem_change_time']    	= date('Y-m-d H:i:s',time());
					$data['problem_surroundings']       = $params['problem_surroundings'];
					$data['problem_change_the_time']    = $params['problem_change_the_time'];
					$data['problem_planned_start_date'] = $params['problem_planned_start_date'];
					$data['problem_planned_end_date']   = $params['problem_planned_start_date'];
					$data['problem_Discovery_Version']  = $params['problem_Discovery_Version'];
					$arr = Db::table('problem')->insert($data);

					$where = array (															
		            	'problem_img'=>$info,
			        );
	        		$sql = Db::table('problem')->where($where)->find();
					$datas['logs_operator'] 		  = session('username');
					$datas['problem_id'] 			  = $sql['problem_id'];
			        $datas['logs_operation_behavior'] = '创建任务';
			        $datas['logs_note'] 			  = '创建项目中出现的问题';
			        $datas['logs_operation_time']     = date('Y-m-d H:i:s',time());
					$arrr = Db::table('logs')->insert($datas);
					return $arr;
                }else{
                    return $file->getError();
                }
            }
        }
        return $array;
    }
}